/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.github.library.bubbleview;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Image;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;
import ohos.media.image.PixelMap;

/**
 * 气泡图片
 *
 * @since 2021-06-24
 */
public class BubbleImage extends Image {

    // 箭头方向
    private BubbleElement.ArrowLocation arrowLocation;

    // 箭头宽度
    private float arrowWidth = BubbleElement.Builder.DEFAULT_ARROW_WITH;

    // 箭头高度
    private float arrowHeight = BubbleElement.Builder.DEFAULT_ARROW_HEIGHT;

    // 箭头位置
    private float arrowPosition = BubbleElement.Builder.DEFAULT_ARROW_POSITION;

    // 箭头是否居中
    private boolean arrowCenter = false;

    // 气泡圆角
    private float angle = BubbleElement.Builder.DEFAULT_ANGLE;

    // 气泡背景色
    private Color bubbleColor = BubbleElement.Builder.DEFAULT_BUBBLE_COLOR;

    /**
     * 构造方法
     *
     * @param context 上下文
     */
    public BubbleImage(Context context) {
        this(context, null);
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 自定义属性
     */
    public BubbleImage(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 自定义属性
     * @param styleName 样式名称
     */
    public BubbleImage(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initView(attrSet);
    }

    private void initView(AttrSet attrSet) {
        if (attrSet != null) {
            if (attrSet.getAttr("arrowLocation").isPresent()) {
                String arrowLocationName = attrSet.getAttr("arrowLocation").get().getStringValue();
                arrowLocation = BubbleElement.ArrowLocation.mapIntToName(arrowLocationName);
            }
            if (attrSet.getAttr("arrowWidth").isPresent()) {
                arrowWidth = attrSet.getAttr("arrowWidth").get().getDimensionValue();
            }
            if (attrSet.getAttr("arrowHeight").isPresent()) {
                arrowHeight = attrSet.getAttr("arrowHeight").get().getDimensionValue();
            }
            if (attrSet.getAttr("arrowPosition").isPresent()) {
                arrowPosition = attrSet.getAttr("arrowPosition").get().getDimensionValue();
            }
            if (attrSet.getAttr("arrowCenter").isPresent()) {
                arrowCenter = attrSet.getAttr("arrowCenter").get().getBoolValue();
            }
            if (attrSet.getAttr("angle").isPresent()) {
                angle = attrSet.getAttr("angle").get().getDimensionValue();
            }
            if (attrSet.getAttr("bubbleColor").isPresent()) {
                bubbleColor = attrSet.getAttr("bubbleColor").get().getColorValue();
            }
        }
        setPadding();
        PixelMap oldPixelMap = getPixelMap();

        addDrawTask((component, canvas) -> {
            RectFloat rectF = new RectFloat(0, 0, getWidth(), getHeight());
            new BubbleElement.Builder()
                    .rect(rectF)
                    .arrowLocation(arrowLocation)
                    .angle(angle)
                    .arrowHeight(arrowHeight)
                    .arrowWidth(arrowWidth)
                    .bubbleColor(bubbleColor)
                    .arrowPosition(arrowPosition)
                    .arrowCenter(arrowCenter)
                    .bubbleType(BubbleElement.BubbleType.PIXEL_MAP)
                    .bubblePixelMap(oldPixelMap)
                    .setCanvas(canvas)
                    .build();
            setPixelMap(null);
        });
    }

    private void setPadding() {
        int left = getPaddingLeft();
        int right = getPaddingRight();
        int top = getPaddingTop();
        int bottom = getPaddingBottom();
        switch (arrowLocation) {
            case LEFT:
                left += arrowWidth;
                break;
            case RIGHT:
                right += arrowWidth;
                break;
            case TOP:
                top += arrowHeight;
                break;
            case BOTTOM:
                bottom += arrowHeight;
                break;
            default:
                break;
        }
        setPadding(left, top, right, bottom);
    }
}
